/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package rentcar.datareceiver.controllers;

import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import rentcar.datareceiver.Application;
import static rentcar.datareceiver.Application.g_SocketList;
import rentcar.datareceiver.models.CmdInfo;
import rentcar.datareceiver.models.TelematicsCmd;
import rentcar.datareceiver.models.TelematicsPara;

/**
 *
 * @author wuhao
 */
@RestController
public class CarInfoController {

        boolean SocketIsConnected(Socket socket) {
                try {
                        socket.sendUrgentData(0xFF);
                        return true;
                } catch (Exception ex) {
                        return false;
                }
        }

        @CrossOrigin
        @RequestMapping("/invokectrl")
        String invokectrl(@RequestParam(value = "vin", required = true) String vin,
                @RequestParam(value = "cmdid", required = true) String cmdid,
                @RequestParam(value = "param", required = false) String param) {

                return OnInvokeCtrl(vin, cmdid, param);
                //return OnSampleInvoke(vin, cmdid, param);
        }

        String OnSampleInvoke(String vin, String cmdid, String param) {
                String strRes = "";
                if ("opendoor".equals(cmdid)) {
                        strRes = "0";
                } else if ("closedoor".equals(cmdid)) {
                        strRes = "0";
                } else {
                        strRes = "-1";
                }
                return "{\"ret\":\"" + strRes + "\"}";
        }

        String OnInvokeCtrl(String vin, String cmdid, String param) {
                System.out.println("invokectrl...");
                String strRes = "-1";
                Gson gson = new Gson();
                Socket client = null;
                for (int i = 0; i < g_SocketList.size(); i++) {
                        if (g_SocketList.get(i) != null && SocketIsConnected(g_SocketList.get(i))) {
                                client = g_SocketList.get(i);
                                break;
                        }
                }

                if (client == null) {
                        System.out.println("socket 连接为空！");
                } else {
                        try {
                                //获取Socket的输出流，用来向客户端发送数据
                                PrintStream out = new PrintStream(client.getOutputStream());
                                //发送到对应的客户端    
                                String OpenDoorCmd = GetOpenDoorCmd(vin, cmdid);
                                System.out.println("Sending command: " + OpenDoorCmd);
                                out.println(OpenDoorCmd);
                                // out.close();
                                System.out.println("sent open command, requiring execution result!");

                                try {
                                        Thread.sleep(2000);
                                } catch (InterruptedException ex) {
                                        Logger.getLogger(CarInfoController.class.getName()).log(Level.SEVERE, null, ex);
                                }

                                System.out.println("getting g_DoorSucceed");
                                if (Application.g_DoorSucceed) {
                                        strRes = "0";
                                }

                        } catch (IOException ex) {
                                System.out.println("Sending command error!");
                                Logger.getLogger(CarInfoController.class.getName()).log(Level.SEVERE, null, ex);
                        }
                }
                return "{\"ret\":\"" + strRes + "\"}";
        }

        String GetOpenDoorCmd(String vin, String cmdid) {

                TelematicsCmd tc = new TelematicsCmd();
                tc.setParam(GetCmdParam(cmdid));

                CmdInfo tp = new CmdInfo();
                tp.setSystemno(vin);
                tp.setIdentifying("E03");
                tp.setData(tc);

                Gson gson = new Gson();
                return gson.toJson(tp);
        }

        String GetCmdParam(String cmdid) {
                String Para = "1";
                switch (cmdid.toLowerCase()) {
                        case "opendoor":
                                Para = "3";
                                break;
                        case "closedoor":
                                Para = "4";
                                break;
                        case "lendcar":
                                Para = "1";
                                break;
                        case "returncar":
                                Para = "2";
                                break;
                        default:
                                break;
                }
                return Para;
        }

        @CrossOrigin
        @RequestMapping("/queryinfo")
        String queryinfo(@RequestParam(value = "vin", required = true) String vin,
                @RequestParam(value = "infoid", required = true) String infoid,
                @RequestParam(value = "param", required = false) String param) {

                String status = "none";
                if ("door".equals(infoid)) {
                        status = "close";
                }
                String strRes = "{\"ret\":\"0\",\"result\":{\"value\":\"" + status + "\"}}";
                return strRes;
        }

}
